package com.hqj.demo;

/**
 * 内部类
 */
public class Test02 {
    public static void main(String[] args) {
        //访问成员内部类
        Outer1 o1 = new Outer1();
        //o1.new Inner1().print();

        //访问局部内部类  - 无法访问
        Outer2 o2 = new Outer2();
        //o2.method();

        //访问匿名内部类
        Outer4 o4 = new Outer4();
        //System.out.println(o4.h.sleep());
        //o4.method();
    }
}


class Outer1{
    private String name = "成员外部类";

    public void method(){
        System.out.println("名称:"+name);
        System.out.println(new Inner1().age);
    }

    class Inner1{
        private int age = 23;

        public void print(){
            System.out.println(name);
        }
    }
}

class Outer2{
    private String name = "局部内部类";
    public static String sex01 = "男";
    public void method(){
        //System.out.println(new Inner2().age);
        class Inner2{
            private int age = 10;
            public void print(){
                System.out.println(name);
            }
        }
    }
}

class Outer3{
    private String name = "静态内部类";
    private static String sex = "男";
    public void method(){
       new Inner3().print();
    }

    static class Inner3{
        private int age = 10;
        public void print(){
            System.out.println(sex+"-"+Outer2.sex01);//只能访问静态的变量
        }
    }
}

interface Home{
    public String sleep();
}

class Outer4{
    private String name = "匿名内部类";
    //接口对象进行匿名创建
    Home h = new Home() {
        @Override
        public String sleep() {
            return "安心助睡眠";
        }
    };

    public void method(){
        //接口对象进行匿名创建
       new Home() {
            @Override
            public String sleep() {
                return "安心助睡眠";
            }
        };
    }

}

